package com.myraceengineer;



import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.ListActivity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.caverock.androidsvg.SVG;
import com.caverock.androidsvg.SVGParseException;
import com.myraceengineer.adapter.SegmentListAdapter;
import com.myraceengineer.model.Segment;


public class MapActivity extends ListActivity {

	private String curentlyHighlightedSeg;
	private SVG svg;
	
	private GraphicalView mChart;

	ViewSwitcher viewSwitcher;
	
	private String[] mMonth = new String[] {
	          "Jan", "Feb" , "Mar", "Apr", "May", "Jun", "Jul", "Aug" 
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map2);

	    //openChart();
	    
		 ImageView  imageView=(ImageView) findViewById(R.id.svgImage);
		  imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		
	      try
	      {
	        svg = SVG.getFromResource(this, R.raw.circuit3);
	        Drawable drawable = new PictureDrawable(svg.renderToPicture());
	        
	         imageView.setImageDrawable(drawable);
	      }
	      catch(SVGParseException e)
	      {Log.e("ERROR", "error",e);
	      }
		List<Segment> values=new ArrayList<Segment>();
		for (int i=1;i<=17;i++) {
			values.add(new Segment("seg"+i,Integer.toString(i),"",LabelsManager.labels[i*8+3],LabelsManager.labels[i*8+5],LabelsManager.labels[i*8+7]));
		}

	      setListAdapter(new SegmentListAdapter(getApplicationContext(), values));
	      curentlyHighlightedSeg="track";
	      
	      ImageView graphView=(ImageView) findViewById(R.id.graph);
	      graphView.setTag(1);
	      graphView.setOnClickListener(new OnClickListener() {
	    	    public void onClick(View v) {
	    	       Log.d("Map", ((ImageView) findViewById(R.id.graph)).getTag().toString());
	    	       if(((ImageView) findViewById(R.id.graph)).getTag() == null || ((ImageView) findViewById(R.id.graph)).getTag() == (Object) 1) {
	    	    	   Log.d("Map", "In 1");
	    	    	   ((ImageView) findViewById(R.id.graph)).setImageResource(R.drawable.graph2);
	    	    	   ((ImageView) findViewById(R.id.graph)).setTag(2);
	    	       }
	    	       else if(((ImageView) findViewById(R.id.graph)).getTag() == (Object) 2) {
	    	    	   ((ImageView) findViewById(R.id.graph)).setImageResource(R.drawable.graph1);
	    	    	   ((ImageView) findViewById(R.id.graph)).setTag(1);
	    	       }
	    	    }
	    	});
	      
	   }



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.map, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}



	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		if (curentlyHighlightedSeg != null) {
			  svg.changeStyle(curentlyHighlightedSeg, "000000");
			  if ("track".equals(curentlyHighlightedSeg)) {
					svg.changeStyle("track", 0);
				}
		}
		String segId=((SegmentListAdapter) getListAdapter()).getItem(position).getId();
		curentlyHighlightedSeg=segId;
		svg.changeStyle(segId, "FF0000");
		if (segId.equals("track")) {
			svg.changeStyle("track", 255.0f);
		}
		
		
		 Drawable drawable = new PictureDrawable(svg.renderToPicture());
		 ImageView  imageView=(ImageView) findViewById(R.id.svgImage);
		 imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
         imageView.setImageDrawable(drawable);

         Log.d("Map", ((Integer) position).toString());
         Log.d("Map", LabelsManager.labels[position * 8 + 0]);
         ((TextView)findViewById(R.id.textView1)).setText(LabelsManager.labels[position * 8 + 0]);
         ((TextView)findViewById(R.id.textView2)).setText(LabelsManager.labels[position * 8 + 1]);
         ((TextView)findViewById(R.id.textView3)).setText(LabelsManager.labels[position * 8 + 2]);
         ((TextView)findViewById(R.id.textView4)).setText(LabelsManager.labels[position * 8 + 3]);
         ((TextView)findViewById(R.id.textView5)).setText(LabelsManager.labels[position * 8 + 4]);
         ((TextView)findViewById(R.id.textView6)).setText(LabelsManager.labels[position * 8 + 5]);
         ((TextView)findViewById(R.id.textView7)).setText(LabelsManager.labels[position * 8 + 6]);
         ((TextView)findViewById(R.id.textView8)).setText(LabelsManager.labels[position * 8 + 7]);
         
         viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);
         viewSwitcher.showNext();

	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK && viewSwitcher.getCurrentView().getId() == R.id.graph)) { //Back key pressed
	        // Things to Do
	    	Log.d("Map", "Detect the back button.");
	    	viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);
	        viewSwitcher.showNext();
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	private void openImage(String name)
	{
		
	}
	
	private void openChart()
	{
		 // Define the number of elements you want in the chart.
	     int z[]={0,1,2,3,4,5,6,7};
	     
	     
	     int x[]={10,18,32,21,48,60,53,80};
	     

	      // Create XY Series for X Series.
	      XYSeries xSeries=new XYSeries("X Series");
	     

	      //  Adding data to the X Series.
	     for(int i=0;i<z.length;i++)
	     {
	    	 xSeries.add(z[i],x[i]);	   
	     }

	        // Create a Dataset to hold the XSeries.
	     
	     XYMultipleSeriesDataset dataset=new XYMultipleSeriesDataset();
	     
	     // Add X series to the Dataset.   
	     dataset.addSeries(xSeries);
	     
	     
	     // Create XYSeriesRenderer to customize XSeries

	     XYSeriesRenderer Xrenderer=new XYSeriesRenderer();
	     Xrenderer.setColor(Color.GREEN);
	     Xrenderer.setPointStyle(PointStyle.DIAMOND);
	     Xrenderer.setDisplayChartValues(true);
	     Xrenderer.setLineWidth(2);
	     Xrenderer.setFillPoints(true);
	     
	     // Create XYMultipleSeriesRenderer to customize the whole chart

	     XYMultipleSeriesRenderer mRenderer=new XYMultipleSeriesRenderer();
	     
	     mRenderer.setChartTitle("X Vs Y Chart");
	     mRenderer.setXTitle("X Values");
	     mRenderer.setYTitle("Y Values");
	     mRenderer.setZoomButtonsVisible(true);
	     mRenderer.setXLabels(0);
	     mRenderer.setPanEnabled(false);
	   
	     mRenderer.setShowGrid(true);
	 
	     mRenderer.setClickEnabled(true);
	     
	     for(int i=0;i<z.length;i++)
	     {
	      mRenderer.addXTextLabel(i, mMonth[i]);
	     }
	     
	       // Adding the XSeriesRenderer to the MultipleRenderer. 
	     mRenderer.addSeriesRenderer(Xrenderer);
	     
	     LinearLayout chart_container=(LinearLayout)findViewById(R.id.Chart_layout);

	     // Creating an intent to plot line chart using dataset and multipleRenderer
	       
	     mChart = ChartFactory.getLineChartView(getBaseContext(), dataset, mRenderer);
	     
	     // Add the graphical view mChart object into the Linear layout .
	     chart_container.addView(mChart);
	}
	
}
